package com.lagou.wwyan.rpc.bean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class RpcRequestMonitor {

    private static final Map<String,RequestTime> REQUEST_TIME = new ConcurrentHashMap<>();
    private static final Map<String,RequestTime> LAST_REQUEST_TIME = new ConcurrentHashMap<>();

    public static void add(String host,int port){
        RequestTime value = new RequestTime(host, port, 0L, 0L);
        LAST_REQUEST_TIME.put(getKey(host, port), value);
    }

    public static void remove(String host,int port){
        LAST_REQUEST_TIME.remove(getKey(host,port));
    }

    private static String getKey(String host, int port) {
        return host + ":" + port;
    }

    public static void start(String requestId,String host,int port){
        REQUEST_TIME.put(requestId,new RequestTime(host,port,System.currentTimeMillis(),0L));
    }
    public static void end(String requestId){
        RequestTime time = REQUEST_TIME.remove(requestId);
        String key = getKey(time.getHost(), time.getPort());
        time.setTimeLength(System.currentTimeMillis()-time.getStart());
        LAST_REQUEST_TIME.put(key,time);

    }
    public static List<RequestTime> getTimes(){
        List<RequestTime> list = new ArrayList<>();
        for (RequestTime value : LAST_REQUEST_TIME.values()) {
            list.add(new RequestTime(value.getHost(),value.getPort(),value.start,value.timeLength));
        }
        return list;
    }
    public static void update(RequestTime time){
        LAST_REQUEST_TIME.put(getKey(time.getHost(),time.getPort()),time);
    }


    public static class RequestTime implements Comparable<RequestTime>{
        private String host;
        private int port;
        private Long start;
        private Long timeLength;

        public RequestTime(String host, int port, Long start, Long timeLength) {
            this.host = host;
            this.port = port;
            this.start = start;
            this.timeLength = timeLength;
        }

        public String getHost() {
            return host;
        }

        public void setHost(String host) {
            this.host = host;
        }

        public int getPort() {
            return port;
        }

        public void setPort(int port) {
            this.port = port;
        }

        public Long getStart() {
            return start;
        }

        public void setStart(Long start) {
            this.start = start;
        }

        public Long getTimeLength() {
            return timeLength;
        }

        public void setTimeLength(Long timeLength) {
            this.timeLength = timeLength;
        }


        @Override
        public int compareTo(RequestTime o) {
            //根据时间和时长排序
       /*     int i = start.compareTo(o.getStart());
            if (i == 0){
                return timeLength.compareTo(o.getTimeLength());
            }
            return i;*/
            return timeLength.compareTo(o.getTimeLength());
        }

        @Override
        public String toString() {
            return "RequestTime{" +
                    "host='" + host + '\'' +
                    ", port=" + port +
                    ", start=" + start +
                    ", timeLength=" + timeLength +
                    '}';
        }
    }
}
